package com.cgsd.heyu.ui.Integral;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.android.library.adapter.BaseAdapter;
import com.android.library.adapter.ClickItemListener;
import com.android.library.adapter.ViewHolder;
import com.cgsd.heyu.R;
import com.cgsd.heyu.adapter.Vp2FragAdapter;
import com.cgsd.heyu.entity.TabEntity;
import com.cgsd.heyu.presenter.home.HomeContract;
import com.cgsd.heyu.ui.BaseActivity;
import com.cgsd.heyu.ui.BaseFragment;
import com.cgsd.heyu.ui.Integral.fragment.IntegralDetailFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaoxing
 * Description 积分明细
 * CreateData: 2021/1/30
 */
public class IntegralDetailActivity extends BaseActivity<HomeContract.Presenter> implements ClickItemListener {
    private TextView tvIntegral;
    private List<BaseFragment> fragments = new ArrayList<>();
    private ViewPager2 mViewPager2;
    private BaseAdapter<TabEntity> adapter;
    private int currentPosition;

    public static void actionStart(Activity activity,String useIntegral){
        Intent intent = new Intent(activity,IntegralDetailActivity.class);
        intent.putExtra("useIntegral",useIntegral);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_integral_detail;
    }

    @Override
    protected void initView() {
        tvIntegral = findViewById(R.id.tv_integral);
        mViewPager2 = findViewById(R.id.mViewPager);
        mViewPager2.setOffscreenPageLimit(3);
        mViewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                currentPosition = position;
                adapter.notifyDataSetChanged();
            }
        });

        RecyclerView mRecyclerViewTab = findViewById(R.id.mRecyclerView_tab);
        mRecyclerViewTab.setLayoutManager(new GridLayoutManager(this,3));
        List<TabEntity> data = new ArrayList<>();
        data.add(new TabEntity("全部记录"));
        data.add(new TabEntity("收入记录"));
        data.add(new TabEntity("支出记录"));
        adapter = new BaseAdapter<TabEntity>(this,R.layout.item_integral_detail_tab,data) {
            @Override
            public void convert(ViewHolder holder, TabEntity tabEntity, int position) {
                TextView tvTitle = holder.getView(R.id.tv_title);
                View line = holder.getView(R.id.view_line);
                tvTitle.setText(tabEntity.title);
                if (currentPosition == position){
                    line.setVisibility(View.VISIBLE);
                    tvTitle.setTextColor(Color.parseColor("#33B9CC"));
                }else {
                    line.setVisibility(View.INVISIBLE);
                    tvTitle.setTextColor(Color.parseColor("#999999"));
                }
            }
        };
        mRecyclerViewTab.setAdapter(adapter);
        adapter.setClickItemListener(this);
    }

    @Override
    protected void initData() {
        String useIntegral = getIntent().getStringExtra("useIntegral");
        tvIntegral.setText(useIntegral);
        fragments.add(IntegralDetailFragment.newInstance(IntegralDetailFragment.TYPE_ALL));
        fragments.add(IntegralDetailFragment.newInstance(IntegralDetailFragment.TYPE_INCOME));
        fragments.add(IntegralDetailFragment.newInstance(IntegralDetailFragment.TYPE_EXPEND));
        Vp2FragAdapter adapter = new Vp2FragAdapter(this,fragments);
        mViewPager2.setAdapter(adapter);
    }

    @Override
    public void clickItem(View view, int position) {
        if (currentPosition != position){
            currentPosition = position;
            adapter.notifyDataSetChanged();
            mViewPager2.setCurrentItem(position);
        }
    }
}
